#!/usr/bin/env python 3.6
# -*- coding: utf-8 -*-
# @Time      :2019/2/14
# @Author    :Tian Maohong
# @Email     :tianmaohong@bbdservice.com
# @File      :R46_TX_EntCurrBadTaxCheck
# @Software  :PyCharm


import json
import re
import sys
import datetime

'''eof
name:企业纳税当前欠税校验
code:R46_TX_EntCurrBadTaxCheck
tableName:
columnName:
groups:场景业务校验-税务
dependencies:TX_CQ_DSJ
type:常用指标
datasourceType:在线指标
description:
eof'''


# reload(sys)
# sys.setdefaultencoding('utf-8')


def inquireIsReportNo(data):
    """
    判断是否存在reportNo
    :param data:
    :return:True/False
    """
    try:

        is_reportNo = True if len(data.get("reportNo")) > 0 else False

    except:
        is_reportNo = False

    return is_reportNo


def formatData(table_Name):
    """
    获取数据
    :param table_Name:字典keys
    :return:[{}]
    """
    try:
        data = TX_CQ_DSJ["data"].get(table_Name)
        return data if isinstance(data, list) and len(data) > 0 else [{}]

    except:
        return [{}]


def convertDataType(data_value, data_type):
    """
    数据格式转换
    :param data_value:
    :param data_type: float/int/str/date_time
    :return:
    """
    data_value = str(data_value)
    return_data = None
    # float
    if data_type == 'float':
        try:
            return_data = float(data_value) if len(data_value) >= 0 else None
        except:
            return_data = None
    # int
    elif data_type == 'int':
        try:
            return_data = int(data_value) if len(data_value) >= 0 else None
        except:
            return_data = None
    # str
    elif data_type == 'str':
        try:
            return_data = str(data_value) if len(data_value) >= 0 else None
        except:
            return_data = None
    # date_time
    elif data_type == 'date_time':
        r = re.compile(r'\D+')
        try:
            return_data = datetime.datetime.strptime(data_value, '%Y.%m.%d').strftime('%Y-%m-%d')
        except:
            try:
                return_data = datetime.datetime.strptime(data_value, '%Y-%m-%d').strftime('%Y-%m-%d')
            except:
                try:
                    return_data = datetime.datetime.strptime(data_value, '%Y/%m/%d').strftime('%Y-%m-%d')
                except:
                    try:
                        data_value = r.sub('', data_value)
                        return_data = datetime.datetime.strptime(data_value, '%Y%m%d').strftime('%Y-%m-%d')
                    except:
                        return_data = None

    return return_data

xmlist = [u"增值税",u"消费税",u"营业税",u"企业所得税",u"企业所得税退税",u"个人所得税",u"资源税",u"城市维护建设税",u"房产税",u"印花税",u"城镇土地使用税",u"土地增值税",u"车船税",u"船舶吨税",u"车辆购置税",u"关税",u"耕地占用税",u"契税",u"烟叶税",u"环境保护税"]
def entCurrBadTaxCheck():
    """
    企业不良纳税记录校验（当前欠税）

    :return:
    """
    syptQsye = formatData('syptQsye')

    if syptQsye == [{}]:
        return 0
    else:
        ownSEList = []

        for i in range(len(syptQsye)):
            SE = convertDataType(syptQsye[i].get('SE'), 'float')
            ZSXM =syptQsye[i].get('zsxm')
            if ZSXM in xmlist and  SE:
                ownSEList.append(SE)
            else:
                ownSEList.append(0)

        return round(sum(ownSEList), 4)


result = entCurrBadTaxCheck()


